import FormConstant from "../../../../js/FormConstant";
export default {
    computed:{
        showButtonData(){
            let list = [];
            this.buttonData.forEach(row=>{
                if(this.showButton(row)){
                    if(row.disableConditions){
                        let disable =this.showButton(row,'disableConditions')
                        row['disable'] = disable
                    }
                    if(row.key=="addSub"){
                        return
                    }
                    list.push(row)
                }
            })
            return list
        }
    },
    methods:{
        /*
        * @param type 是conditions显示，disableConditions禁用条件
         */
        showButton(btn,type){
            if(!type){
                type='conditions'
            }
            if(!this.handleCondition(btn,{},false,type)){
                return false;
            }
            let key=btn.key;
            if(this.buttonPermission[key]==undefined){
                return true;
            }
            return this.buttonPermission[key];
        },
        buttonPermission(){
            return  this.localPermission["buttons"] || {};
        },
        localPermission(){
            let {field } = this.metadata;

            let permission= this.permission && this.permission.subTable && this.permission.subTable[field] || {}

            return permission;
        },
        /**
         * 处理操作列显示条件
         * @param btn 按钮配置
         * @param row 当前行数据(行操作才有)
         * @param isRowBtn 是否为行操作按钮
         * @param type 是conditions显示，disableConditions禁用条件
         * @returns {boolean}
         */
        handleCondition(btn, row, isRowBtn,type) {
            if(!type){
                type='conditions'
            }
            let curUser = uni.getStorageSync("user");
            if (btn[type]) {
                let conditionStr = "";
                let conditions = JSON.parse(btn[type]);
                if (conditions.type == "field") {
                    conditionStr = this.handleFieldCondition(conditions.conditionConfig, isRowBtn);
                } else {
                    conditionStr = conditions.conditionConfig;
                }
                if(!conditionStr){
                    return true;
                }
                if (conditionStr.indexOf('return') === -1) {
                    conditionStr = 'return ' + conditionStr;
                }
                let func = new Function("formData", "row", "user", conditionStr);
                return func(this.formVm.data, row, curUser);
            }
            return true;
        },
        /**
         * 动态先计算出列表操作按钮
         * @param col 行操作列表按钮
         * @param row 行数据
         * @returns {*[]}
         */
        handleConditionList(col,row){
            let rowButtonData = [];
            col.forEach(res=>{
                if(this.handleCondition(res,row,true)){
                    rowButtonData.push(res)
                }
            })
            return rowButtonData
        },
        /**
         * 将显示条件的字段配置转为脚本
         * @param config 字段配置
         * @param isRowBtn 是否为行操作按钮
         * @returns {string}
         */
        handleFieldCondition(config, isRowBtn) {
            let conditionStr = "";
            if(!config.rules || config.rules.length==0){
                return "true";
            }
            for (let i = 0; i < config.rules.length; i++) {
                let rule = config.rules[i];
                if (conditionStr) {
                    conditionStr += config.condition == "OR" ? "||" : "&&"
                }
                if (!rule.condition) {
                    //条件字段
                    let fieldName = "";
                    if (rule.tableName == "curForm") {
                        fieldName = "formData." + rule.fieldName;
                    } else if( FormConstant.SubPrefix+this.itemConfig.field == rule.tableName && isRowBtn) {
                        fieldName = "row." + rule.fieldName;
                    }else {
                        fieldName = "formData."+rule.tableName+"." + rule.fieldName;
                    }
                    //处理多值字段
                    if (rule.valMode != 'single') {
                        if (rule.fieldValue == "value") {
                            conditionStr = fieldName + " && " + conditionStr;
                            fieldName = "(JSON.parse(" + fieldName + ").value)";
                        } else if (rule.fieldValue == "label") {
                            conditionStr = fieldName + " && " + conditionStr;
                            fieldName = "(JSON.parse(" + fieldName + ").label)";
                        }
                    }
                    //值字段
                    let value = "";
                    if (rule.valType == "filed") {
                        value = "row." + rule.value;
                    } else if (rule.valType == "constVar") {
                        value = "user." + rule.value;
                    } else {
                        value = rule.value;
                    }
                    //拼接比较符
                    conditionStr += this.montageFieldOp(rule.fieldOp, fieldName, value, rule.ctlType );
                } else {
                    conditionStr += "(" + this.handleFieldCondition(rule) + ")";
                }
            }
            return conditionStr;
        },
        /**
         * 拼接比较符
         * @param op 比较符
         * @param startField 开始字段
         * @param endField 结束字段
         * @param ctlType 控件类型
         */
        montageFieldOp(op, startField, endField, ctlType) {
            let conditionStr = "";
            if(ctlType!='number'){
                endField = "'"+endField+"'";
            }
            switch (op) {
                case 'EQ':
                    conditionStr = startField + "==" + endField;
                    break;
                case 'NEQ':
                    conditionStr = startField + "!=" + endField;
                    break;
                case 'CONTAIN':
                    conditionStr = startField + ".indexOf(" + endField + ") == 0";
                    break;
                case 'NOTCONTAIN':
                    conditionStr = "!" + startField + ".indexOf(" + endField + ") == -1";
                    break;
                case 'LT':
                    conditionStr = startField + "<" + endField;
                    break;
                case 'LE':
                    conditionStr = startField + "<=" + endField;
                    break;
                case 'GT':
                    conditionStr = startField + ">" + endField;
                    break;
                case 'GE':
                    conditionStr = startField + ">=" + endField;
                    break;
                default:
                    conditionStr = startField + "==" + endField;
                    break;
            }
            return conditionStr;
        }
    }
}
